how to insert image file into MySQL database using PHP
To bring up phpMyAdmin type the following address in your browser:
http://localhost/phpMyAdmin/
Integer Values
TINYINT Signed: -128 to 127. Unsigned: 0 to 255
SMALLINT Signed: -32768 to 32767. Unsigned: 0 to 65535
MEDIUMINT Signed: -8388608 to 8388607. Unsigned: 0 to 16777215
INT Signed: -2147483648 to 2147483647. Unsigned: 0 to 4294967295
BIGINT Signed: -9223372036854775808. Unsigned: 0 to 18446744073709551615
Text Types
The length for the text types can be quite confusing. The MySQL manual says this about the various lengths that each text type can hold:
TINYTEXT L+1 byte, where L < 2^8
TEXT L+2 bytes, where L < 2^16
MEDIUMTEXT L+3 bytes, where L < 2^24
LONGTEXT L+4 bytes, where L < 2^32
This in not terribly helpful for beginners! So what does it mean. Well, the L + 1 part means, "The length of the string, plus 1 byte to store the value." The translated values for each are approximately:
TINYTEXT 256 bytes
TEXT 64 KiloBytes
MEDIUMTEXT 16 MegaBytes
LONGTEXT 4 GigaBytes
To confuse the issue even more, you can also use CHAR and VARCHAR to store your text. These are quite useful, if you know how many characters you want to store. For example, for a UK postcode you don't need more than 9 characters, and one of those will be a blank space. So there's no sense in setting a postcode field to hold 4 gigabytes! Instead, use CHAR or VARCHAR.
CHAR
You specify how many characters you want the field to hold. The maximum value is 255. For example:
CHAR(10)
This field can then hold a maximum of ten characters. But if you only use 4 of them, the rest of the 10 characters will be blank spaces. The blank spaces get added to the right of your text:
"TEXT "
"TENLETTERS"
VARCHAR
Like CHAR, but the rest of the characters are not padded with blank spaces. The maximum value before MySQL 5.0.3 was 255. After this it's jumped to 65, 535. With VARCHAR, there is also an extra byte that records how long your text is.
Open and Close MySQL database:
inbuilt functions
mysql_connect()
mysql_select_db()
mysql_close()
The approach we'll take has three steps:
- Open a connection to MySQL itself
- Specify the database we want to open
- Close the connection
Step 1 - Open a connection to MySQL
The first job is to actually connect to MySQL. As it's name suggests, mysql_connect( ) does exactly that. Here's the code we're going to be using. But this is just to get your started. It is recommended that you don't dash off and use this on the internet! This is for learning purposes only.
<?PHP
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);
print "Connection to the Server opened";
?>
Save your work and try it out on your server (this assumes that you have the Address Book database we created earlier, and that it is in the data folder of MySQL. If you don't, you can download all the files here.).
The first four lines are just setting up variables, and putting something in them:
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
The username we're trying here is "root" and the password is blank. These are the MySQL defaults. You don't need to change these, in most cases.
Hopefully, you won't have any errors. But the line that connects to MySQL is this:
mysql_connect($server, $user_name, $password);
So you type the name of the function first ( mysql_connect ), followed by the round brackets. In between the round brackets, you need three things: the name of your server, your MySQL username, and your MySQL password. These can be entered directly, like this:
mysql_connect( '127.0.0.1', 'root', '' );
Or as variables, like we did at first:
$user_name = "root";
$password = "";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);
And that's all you need to get you connected to MySQL. But we haven't connected to the database yet. That's Step 2 on our list.
[http://www.homeandlearn.co.uk/php/php13p1.html]
Step 2 - Specify the database you want to open
In our code, we set up a variable with the name of our database:
$database = "addressbook";
We now need to do something with this variable. So add this new line to your code (the new line is in blue text):
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database);
print "Connection to the Server opened";
You use the mysql_select_db( ) function to specify which database you want to open. The function then returns a true/false value. If it finds your database, a value of true is returned; if your database can't be found then a value of false is returned. You can use some logic to test if the database was found. Change the last two lines of your code to this:
$db_found = mysql_select_db($database);
if ($db_found) {
print "Database Found";
}
else {
print "Database NOT Found";
}
Now change the database name from this:
$database = "addressbook";
to something like this:
$database = "addressbook2";
Run your code again, and you should see Database NOT Found printed out (unless you have a database called addressbook2). Change the database name back to addressbook.
But there's another option you can use for mysql_select_db - something called a resource link identifier. It's just a file handle that you used in an earlier section (opening text files). You use it like this:
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
print "Database Found " . $db_handle;
}
else {
print "Database NOT Found " . $db_handle;
}
So when we connect to the database, we're now using this:
$db_handle = mysql_connect($server, $user_name, $password);
It's just the same as before, except we're returning a value from the mysql_connect function, and putting it into a variable called $db_handle. When we connect to the database, we can use this file handle:
$db_found = mysql_select_db($database, $db_handle);
The resource link identifier (file handle) goes after the name of the database you want to open. You can then use this file handle to refer to your database connection.
Now that we've connected to MySQL, and connected to a database, it's time to close the connection.
[http://www.homeandlearn.co.uk/php/php13p1.html]
Step 3 - Close the connection
Closing a connection to a database is quite easy. If you've used a file handle, as above, you just do this:
mysql_close( $db_handle );
Otherwise, you don't need to bother. It's recommended that you take the file handle approach, though. That's what we'll be doing from now on.
So, we'll add a line to close our connection. Here what your code should now look like:
<?PHP
$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
print "Database Found ";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
?>
http://www.homeandlearn.co.uk/php/php13p1.html
Structured Query Language
SQL (pronounced SEEKwel), is a way to query and manipulate databases. The basics are quite easy to learn. If you want to grab all of the records from a table in a database, you use theSELECT word. Like this:
SELECT * FROM Table_Name
SQL is not case sensitive, so the above line could be written:
Select * From Table_Name
But your SQL statements are easier to read if you type the keywords in uppercase letters. The keywords in the lines above are SELECT and FROM. The asterisk (*) means "All Records".Table_Name is the name of a table in your database. So the whole line reads:
"SELECT all the records FROM the table called Table_Name"
http://www.homeandlearn.co.uk/php/php13p2.html
You can see how many rows were returned from the database table.
we attempt to connect to the database:
$user_name = "root";
$pass_word = "";
$database = "login";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
}
else {
$errorMessage = "Error logging on";
The inbuilt function mysql_num_rows( ) is used for this. If no rows were returned, then that tells you that there's something wrong with either the username or password.
$num_rows = mysql_num_rows($result);
Next, we test the $num_rows variable to see if it's greater than zero. If it is, then you have a successful logon. If not, then it's invalid:
if ($num_rows > 0) {
$errorMessage= "logged on ";
}
else {
$errorMessage= "Invalid Logon";
}
if ($db_found) {
}
else {
$errorMessage = "Error logging on";
}
If the database isn't found, then some text is added to the error message variable. If the database was found, strip the incoming text of any unwanted characters (SQL Injection attacks). These next two lines call the function at the top of the code:
$uname = quote_smart($uname, $db_handle);
$pword = quote_smart($pword, $db_handle);
With the username and password sanitised, we can then set up a SQL command. We're selecting all the records in the database where the incoming username and password match the database table fields called L1 and L2:
$SQL = "SELECT * FROM login WHERE L1 = $uname AND L2 = $pword";
Next, issue the SQL command using mysql_query( ):
$result = mysql_query($SQL);
We need to check what is returned by the mysql_query() function. The value in $result will either be true (if any records are returned) or false (if none are returned). We're checking to see if there were any errors when the SQL command was issued against the database table. If so, put something in the error message variable:
if ($result) {
}
else {
$errorMessage = "Error logging on";
}
If the SQL command was issued successfully, you can see how many rows were returned from the database table. The inbuilt function mysql_num_rows( ) is used for this. If no rows were returned, then that tells you that there's something wrong with either the username or password.
$num_rows = mysql_num_rows($result);
Next, we test the $num_rows variable to see if it's greater than zero. If it is, then you have a successful logon. If not, then it's invalid:
if ($num_rows > 0) {
$errorMessage= "logged on ";
}
else {
$errorMessage= "Invalid Logon";
}
In the above code, the number of rows returned could be greater than 1. That would mean that 2 or more people have the same username and password. If you have a website where each user has to be unique, then you obviously want to check if $num_rows = 1. For some websites, it doesn't really matter if 2 or more people have the same login details. But for things like forums, where people are posting and replying to the input of others, then it does matter. After all, you want to credit forum users with the correct posts. For the purpose of this tutorial, assume that it doesn't matter if login details are the same.
Sessions:
<?PHP
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: login.php");
}
?>
This checks to see if the session called login is set, and that it's not a blank string. If it is, then the user is redirected to the login page. In the script, you first start the session:
session_start();
Next comes a complex If statement:
if () {
header ("Location: login.php");
}
In between the round brackets of the If statement, we have the NOT operator. This is followed by the inbuilt isset() function:
if ( !(isset( ) ) {
}
This says, "If NOT isset". Or, "if the value of the isset function is false ... " If the value in the round brackets of isset is indeed false, then the code between the curly brackets { } gets executed. That code, for us, was the redirection line. What we have between the round brackets of isset is this:
($_SESSION['login'])
That's just our session variable from the login page. Is the user has logged in successfully, a value of 1 will be set inside of this variable.
But we also need to check the session variable for a blank string. So we have and AND part to the statement:
&& $_SESSION['login'] != ''
This says, "AND session login DOES NOT EQUAL a blank string". In other words, we check to see if a session variable has been set, and that it's not a blank string.
If everything is OK then the user will see the HTML code below the PHP at the top. If it's not, you can send them somewhere else. But you need to put that PHP code at the top of every page that you want to protect. And it needs to go before any HTML code. You can't put it in the head section, otherwise you'll get "header" errors.
http://www.homeandlearn.co.uk/php/php14p3.html
echo
print
- Outputs only a single string
-
Returns 1, so it can be used in an expression
e.g. print "Hello"
or, if ($expr && print "foo")
print_r()
- Outputs a human-readable representation of any one value
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Useful when debugging
- May return its output as a return value (instead of echoing) if the second optional argument is given
var_dump()
- Outputs a human-readable representation of one or more values separated by commas
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Uses a different output format to print_r(), for example it also prints the type of values
- Useful when debugging
- No return value
var_export()
- Outputs a human-readable and PHP-executable representation of any one value
- Accepts not just strings but other types including arrays and objects, formatting them to be readable
- Uses a different output format to both print_r() and var_dump() - resulting output is valid PHP code!
- Useful when debugging
- May return its output as a return value (instead of echoing) if the second optional argument is given
Notes:
- Even though print can be used in an expression, I recommend people avoid doing so, because it is bad for code readability (and because it's unlikely to ever be useful). The precedence rules when it interacts with other operators can also be confusing. Because of this, I personally don't ever have a reason to use it over echo.
- Whereas echo and print are language constructs, print_r() and var_dump()/var_export() are regular functions. You don't need parentheses to enclose the arguments to echo or print (and if you do use them, they'll be treated as they would in an expression).
http://stackoverflow.com/questions/1647322/whats-the-difference-between-echo-print-and-print-r-in-php
Which MySQL datatype use for store an IP address?
If you want to store IP address in mysql database then don’t mistake to use varchar datatype because you can use INT UNSIGNED 4(BYTE) datatype. using integer datatype you can save more space in database.
when you fire insert query at that time use INET_ATON() and select query at that time INET_NTOA() use. how to use this function given bellow example.
Create Table :
CREATE TABLE IF NOT EXISTS `ip_addresses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`ip_address` INT(4) UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
);
Insert Data :
INSERT INTO `ip_addresses` (`ip_address`) VALUES (INET_ATON("127.0.0.1"));
Select Data :
SELECT id, INET_NTOA(`ip_address`) as ip FROM `ip_addresses`;
Try this, this is a very simple.......
http://itsolutionstuff.com/post/which-mysql-datatype-use-for-store-an-ip-address
Retrieving Data From More Than One Table